package com.xiaoshuidi.cloud.module.member.controller.admin.coupon;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.coupon.vo.*;
import com.xiaoshuidi.cloud.module.member.controller.app.coupon.vo.CouponCustomerVo;
import com.xiaoshuidi.cloud.module.member.convert.coupon.CouponConvert;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.enums.coupon.CouponUsageStatusEnum;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponCustomerService;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponRecordService;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponService;
import com.xiaoshuidi.cloud.module.member.utils.CommonIdVo;
import com.xiaoshuidi.cloud.module.member.utils.CommonTenantIdVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.USER_PHONE_NOT_NULL;

/**
 * @version generator
 * @description 客户优惠券
 * @date 2023-03-15 15:02:02
 */
@Tag(name = "管理后台 - 发券（租客相关）活动")
@RestController
@RequestMapping(ApiConstants.NAME + "/member")
public class CouponCustomerController {

    @Autowired
    CouponCustomerService service;
    @Autowired
    CouponService couponService;
    @Autowired
    CouponCustomerService couponCustomerService;
    @Resource
    CouponRecordService couponRecordService;

    @Operation(summary = "优惠活动下拉")
    @GetMapping("/coupon-select")
    public CommonResult<List<CouponSelectVo>> couponSelect() {
        List<CouponSelectVo> list = couponService.couponSelect();
        return CommonResult.success(list);
    }


    @PostMapping("/send-customer-coupon")
    @Operation(summary = "发优惠券")
    public CommonResult<List<CouponCustomerVo>> sendCustomerCoupon(@RequestBody CouponCustomerPCForm form) {
        if (form.getUserPhone() == null) {
            throw exception(USER_PHONE_NOT_NULL);
        }
        couponService.sendCustomerCoupon(form);
        return CommonResult.success();
    }

    @Operation(summary = "发券历史")
    @PostMapping("/get-send-records")
    public CommonResult<PageResult<CouponRecordVo>> getSendRecords(@RequestBody CouponRecordForm form) {
        PageResult<CouponRecordVo> pageData = couponRecordService.couponRecord(form);
        return CommonResult.success(pageData);
    }

    @PostMapping("/customer-coupon-list")
    @Operation(summary = "优惠券明细")
    public CommonResult<PageResult<CouponCustomerVo>> getCustomerCouponList(@RequestBody CouponCustomerPageForm form) {
        PageResult<CouponCustomerVo> pageData = couponCustomerService.getCustomerCouponList(form);
        return CommonResult.success(pageData);
    }

    @PostMapping("/export-customer-coupon-list")
    @Operation(summary = "优惠券明细导出")
    public void exportCustomerCouponList(@RequestBody CouponCustomerPageForm form, HttpServletResponse response) throws IOException {
        List<CouponCustomerVo> list = couponCustomerService.exportCustomerCouponList(form);
        List<CouponCustomerExportVo> exportVos = list.stream().map(c -> {
            CouponCustomerExportVo vo = new CouponCustomerExportVo();
            BeanUtils.copyProperties(c, vo);
            vo.setUsageStatus(CouponUsageStatusEnum.getDesc(c.getUsageStatus()));
            return vo;
        }).collect(Collectors.toList());
        ExcelUtils.write(response, "优惠券明细.xls", "优惠券明细", CouponCustomerExportVo.class, exportVos);
    }

    @PostMapping("/disable-customer-coupon")
    @Operation(summary = "优惠券作废")
    public CommonResult<Boolean> disableCustomerCoupon(@RequestBody CommonIdVo form) {
        couponCustomerService.cancel(form.getId());
        return CommonResult.success(Boolean.TRUE);
    }

    @PostMapping("/coupon-customer-use")
    @Operation(summary = "优惠券核销")
    public CommonResult<Boolean> couponCustomerUse(@RequestBody CouponCustomerUseForm form) {
        service.couponCustomerUse(form);
        return CommonResult.success();
    }


    @PostMapping("/query-room-contract")
    @Operation(summary = "定时自动发券时间")
    public CommonResult<Boolean> queryRoomContract() {
        LocalDateTime now = LocalDateTime.now();
        //查询已签约的合同
//		List<RoomContractAndBookingDto> roomContractAndBookingDtos = roomContractService.queryReservationRoomContract(now);

        return CommonResult.success();
    }

    @PostMapping("/get-member-coupons")
    @Operation(summary = "个人客户优惠券列表")
    public CommonResult<PageResult<CouponCustomerVo>> getTenantCoupons(@RequestBody CommonTenantIdVo vo) {
        PageResult<CouponCustomerVo> page = couponCustomerService.getTenantCoupons(vo);
        return CommonResult.success(page);
    }

    @PostMapping("/get-coupons-by-bill/{billId}")
    @Operation(summary = "根据账单id获取优惠券信息")
    public CommonResult<List<CouponCustomerVo>> getCouponsByBill(@PathVariable("billId") String billId) {
        List<CouponCustomerVo> page = couponCustomerService.getCouponsByBill(billId);
        return CommonResult.success(page);
    }

}

